package com.toolbox.dal.repository.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toolbox.bo.AccountBO;
import com.toolbox.dal.repository.InfraMenuDORepository;
import com.toolbox.dal.mapper.InfraMenuMapper;
import com.toolbox.dal.model.InfraMenuDO;
import com.toolbox.dto.InfraMenuDTO;
import com.toolbox.req.InfraMenuReq;
import com.toolbox.util.CopyBeanUtilExt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 菜单权限 服务实现类
 * </p>
 *
 * @author Mango
 * @since 2023-12-01
 */
@SuppressWarnings("ALL")
@Slf4j
@Service
public class InfraMenuDORepositoryImpl extends ServiceImpl<InfraMenuMapper, InfraMenuDO> implements InfraMenuDORepository {

    @Autowired
    private InfraMenuMapper mapper;

    @Override
    public List<InfraMenuDTO> getMenuNodes() {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Long createMenu(InfraMenuReq req, AccountBO bo) {
        InfraMenuDO entityMenu = CopyBeanUtilExt.dtoToDo(req, InfraMenuDO.class);

        entityMenu.setCreatorId(bo.getUserId());
        entityMenu.setCreatorName(bo.getNickname());

        entityMenu.setModifierId(bo.getUserId());
        entityMenu.setModifierName(bo.getNickname());

        mapper.insert(entityMenu);

        return entityMenu.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Long updateMenuById(InfraMenuReq req, AccountBO bo) {
        InfraMenuDO oldMenu = mapper.selectById(req.getId());

        oldMenu.setParentId(oldMenu.getParentId());
        oldMenu.setNameCn(oldMenu.getNameCn());
        oldMenu.setNameEn(oldMenu.getNameEn());
        oldMenu.setPath(oldMenu.getPath());
        oldMenu.setName(oldMenu.getName());
        oldMenu.setUrl(oldMenu.getUrl());
        oldMenu.setPermission(oldMenu.getPermission());
        oldMenu.setType(oldMenu.getType());
        oldMenu.setIcon(oldMenu.getIcon());
        oldMenu.setIsShow(oldMenu.getIsShow());
        oldMenu.setTab(oldMenu.getTab());
        oldMenu.setMultiple(oldMenu.getMultiple());
        oldMenu.setKeepalive(oldMenu.getKeepalive());
        oldMenu.setSort(oldMenu.getSort());

        oldMenu.setModifierId(bo.getUserId());
        oldMenu.setModifierName(bo.getNickname());

        mapper.updateById(oldMenu);

        return oldMenu.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void removeById(Long id) {
        mapper.deleteById(id);
    }

}
